package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.TreeBuildUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StDeviceCategeryBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StDeviceCategeryVo;
import com.ruoyi.ruoyiyinzheng.domain.StDeviceCategery;
import com.ruoyi.ruoyiyinzheng.mapper.StDeviceCategeryMapper;
import com.ruoyi.ruoyiyinzheng.service.IStDeviceCategeryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备类别（树形）Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
public class StDeviceCategeryServiceImpl implements IStDeviceCategeryService {

    private final StDeviceCategeryMapper baseMapper;

    @Override
    public List<StDeviceCategeryVo> selectDeviceCategery(StDeviceCategeryBo stDeviceCategeryBo) {
        LambdaQueryWrapper<StDeviceCategery> lqw = buildQueryWrapper(stDeviceCategeryBo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<Tree<Long>> selectDeviceCategeryreeList(StDeviceCategeryBo stDeviceCategeryBo) {
        List<StDeviceCategeryVo> depts = this.selectDeviceCategery(stDeviceCategeryBo);
        return buildDeviceTreeSelect(depts);
    }

    @Override
    public List<Tree<Long>> buildDeviceTreeSelect(List<StDeviceCategeryVo> stDeviceCategeryVoList) {
        if (CollUtil.isEmpty(stDeviceCategeryVoList)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(stDeviceCategeryVoList, (dept, tree) ->
            tree.setId(dept.getId())
                .setParentId(dept.getParentId())
                .setName(dept.getName()));
    }

    /**
     * 查询设备类别（树形）
     */
    @Override
    public StDeviceCategeryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询设备类别（树形）列表
     */
    @Override
    public List<StDeviceCategeryVo> queryList(StDeviceCategeryBo bo) {
        LambdaQueryWrapper<StDeviceCategery> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StDeviceCategery> buildQueryWrapper(StDeviceCategeryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StDeviceCategery> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, StDeviceCategery::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), StDeviceCategery::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StDeviceCategery::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSimpleName()), StDeviceCategery::getSimpleName, bo.getSimpleName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), StDeviceCategery::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增设备类别（树形）
     */
    @Override
    public Boolean insertByBo(StDeviceCategeryBo bo) {
        StDeviceCategery add = BeanUtil.toBean(bo, StDeviceCategery.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备类别（树形）
     */
    @Override
    public Boolean updateByBo(StDeviceCategeryBo bo) {
        StDeviceCategery update = BeanUtil.toBean(bo, StDeviceCategery.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StDeviceCategery entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备类别（树形）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
